package com.googlecode.curvedmetromaps.core.logic.costfunction;

import com.googlecode.curvedmetromaps.core.model.MetroEdge;
import com.googlecode.curvedmetromaps.core.model.MetroMap;

/**
 * Cost function that keeps adjacent vertices together.
 */
public class AdjacentVertexCost extends BaseCost {

	public AdjacentVertexCost(MetroMap map) {
		super(map);
		createWeight("base", "Base weight", 5);
		createWeight("adjDist", "Adjacent distance", 0.1);
	}

	@Override
	public String getName() {
		return "Adjacent vertices";
	}

	@Override
	public double calculateCost() {
		double result = 0;
		double adjDist = getWeight("adjDist");

		for (MetroEdge edge : map.edgeArray) {
			// cost = -(||p0 - p1||^2 / dvmax^2)
			float dx = edge.src.x - edge.dst.x;
			float dy = edge.src.y - edge.dst.y;
			double dist = Math.sqrt(dx * dx + dy * dy);
			double cost = dist / adjDist;
			result += cost;
		}
		return getWeight("base") * result / map.edgeArray.length;
	}

}
